package com.controller;

import com.alibaba.fastjson2.JSONObject;
import com.pojo.Menu;
import com.pojo.UseClassify;
import com.pojo.UseDefinition;
import com.server.MenuServer;
import com.vo.Purposes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
public class MenuController {
    private final MenuServer menuServer;

    @Autowired
    public MenuController(MenuServer menuServer) {
        this.menuServer = menuServer;

    }

    @RequestMapping("/select/Menu")
    @ResponseBody
    public List<Menu> selectMenu(@RequestBody JSONObject jsonObject) {
        String userName = jsonObject.getString("userName");
        List<Menu> menus = menuServer.selectMenuList(userName);
        for (Menu menu : menus) {
            printMenu(menu);
        }
        return menus;
    }

    private void printMenu(Menu menu) {
        // 输出菜单信息
        // System.out.println("Menu: " + menu.getTitle() + " - " + menu.getIndex());
        List<Menu> children = menu.getChildren();
        if (children != null && !children.isEmpty()) {
            for (Menu child : children) {
                printMenu(child); // 递归打印子菜单
            }
        }
    }
}
